<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/11/25
 * Time: 14:05
 */
namespace app\role\controller;

use app\admin\controller\AdminBase;
use app\common\model\AdminMenu;
use app\common\model\Role;
use app\common\model\RoleUser;
use app\common\model\User;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Admin extends AdminBase
{
    public function index()
    {
        $status = Request::param('status', 1);

        $role_list = getList('role', ['status'=>$status]);
        return View::fetch('index',[
            'status'    => $status,
            'list' => $role_list,
        ]);
    }

    /**
     * 显示添加角色模板
     */
    public function add()
    {
        $admin_menu_list = $this->get_menu(0);

        return View::fetch('add', [
            'admin_menu_list' => $admin_menu_list,
        ]);
    }

    /**
     * 递归得到菜单列表
     * @param number $parent_id
     * @return void|multitype:multitype:
     */
    public function get_menu($parent_id = 0)
    {
        $admin_menu_list = AdminMenu::where(['parent_id' => $parent_id])->select()->toArray();
        if (empty($admin_menu_list)) {
            return;
        }
        $arr = array();
        foreach ($admin_menu_list as $k=>$v) {
            $temp = array();
            $temp = $v;
            $temp['sub'] = $this->get_menu($v['id']);
            $arr[] = $temp;
        }

        return $arr;
    }

    /**
     * 保存
     * @return string
     */
    public function save()
    {
        $id   = Request::param('id', 0);
        $info = Request::param('info', array());
        $privilege = Request::param('privilege', array());
        //an_dump($video_category);die;
        foreach ($info as $k=>$v) {
            $info[$k] = trim($v);
        }

        if (!isset($info['title']) && empty($info['title'])) {
            return '角色名不能为空';
        }

        $info['privilege'] = implode(',', $privilege);
        if (!$id) {
            Role::create($info);
        } else {
            Role::update($info,['id'=>$id]);
        }

        return redirect(url('/role/admin/index'));
    }

    /**
     * 编辑
     * @return string
     */
    public function edit()
    {
        $id = Request::param('id', 0);
        if (!$id) {
            return _return('非法请求');
        }

        $role_info = _uri('role',['id'=>$id]);
        if (!$role_info) {
            return _return('非法请求');
        }

        $admin_menu_list = $this->get_menu(0);

        return View::fetch('add',[
            'info'            => $role_info,
            'admin_menu_list' => $admin_menu_list,
        ]);
    }

    /**
     * 删除角色
     */
    public function delete()
    {
        // 得到id
        $id = Request::param('id');
        // 拆分id
        $ids = explode(',', $id);
        // 判断id是否为int并不为0
        foreach ($ids as $k=>$v) {
            $v = (int)$v;
            if (!$v) {
                continue;
            }
            // 根据id得到信息
            $role_info = _uri('role', ['id'=>$v]);
            if (!$role_info) {
                continue;
            }
            if ($role_info['status']==0) {
                Role::update(['status'=>1], ['id'=>$v]);
            } else {
                Role::update(['status'=>0], ['id'=>$v]);
            }
        }

        return ['info' => 'ok'];
    }

    public function add_user()
    {
        $id = Request::param('id', 0);

//        $filter = array(
//            'firm_id' => 0,
//            'status'  => 1,
//        );
//        $user_list = _model('user')->getList($filter);
//        Response::assign('user_list', $user_list);
//
//        Response::display('add_user.html');
    }
}